from bs4 import BeautifulSoup


def header_placeholder() -> str:
    """
    返回头部占位符HTML
    单行图片时,最大宽度为 950px
    单行两张图片时,图片宽度为 450px
    """
    return """
    <div class="cxsw_header_placeholder">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_1.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_2.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_3.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_4.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_5.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_6.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_7.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_8.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_9.png">
        <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/header/banner_10.png">
    </div>
    """


def add_ad_service() -> str:
    """
    添加广告服务
    """
    return """
    <div class="cxsw_service">
            <a href="https://work.weixin.qq.com/kfid/kfcd1ffef164b328abc" target="_blank" style="text-decoration:none;
    color:#333;display: flex;align-items: center;">
                <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/service.gif"
                    alt="点击立即咨询" srcset="">
            </a>
        </div>
    """


def footer_placeholder() -> str:
    """
    返回底部占位符HTML
    """
    return """
    <div class="cxsw_footer_placeholder">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_1.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_2.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_3.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_4.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_5.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_6.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_7.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_8.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_9.png">
    <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/ad/footer/banner_10.png">
    </div>
    """


def discount_and_round(price, discount: float = 0.9):
    """
    打折并四舍五入取整

    :param price: 原始价格
    :param discount: 折扣数默认0.9
    :return: 返回新折扣
    """
    # 先打七折
    discounted_price = price * discount

    # 将结果转换为字符串以便处理第一位
    price_str = str(discounted_price)

    # 找到小数点的位置
    decimal_index = price_str.find(".")

    if decimal_index != -1:
        # 获取第一位小数
        first_decimal = int(price_str[decimal_index + 1])

        # 如果第一位小数大于等于5，向上取整；否则向下取整
        if first_decimal >= 5:
            rounded_price = int(discounted_price) + 1
        else:
            rounded_price = int(discounted_price)
    else:
        # 如果没有小数点，直接取整
        rounded_price = int(discounted_price)

    return rounded_price


def rj_imglist(url_list: list) -> list:
    """转换rj图片链接列表
    1. 长度大于5时，截取前5。否则用辰星logo图补齐长度
    2. 每一项转换为图片对象
    3. 返回处理结果

    Args:
        url_list (list): 原始图片列表

    Returns:
        list: [{"photo": url},xxxx]
    """
    # 截取或补齐长度
    logo_url = "https://images.rjmart.cn/image/enqu62up/860d4319-09b2-4af2-8cde-3bb94773fdee.jpg"
    url_list = url_list[:5]
    if len(url_list) < 5:
        url_list.extend([logo_url] * (5 - len(url_list)))

    # 将url列表转换为图片对象
    url_list = [{"photo": url} for url in url_list]
    return url_list


def del_old_content(old_content: str) -> str:
    """删除旧内容

    Args:
        content (str): 原始内容
    """
    try:
        soup = BeautifulSoup(old_content, "html.parser")
        del_div = soup.find("div", id="con_header")
        if del_div:
            del_div.extract()
            new_content = (
                str(soup)
                .replace("</img>", "")
                .replace('<img src=""/>', "")
                .replace('<img src="">', "")
            )
            return new_content
        return old_content

    except Exception as e:
        return old_content

def generate_groups(elements, group_size=50):
    """
    生成器函数，按指定大小逐组产生元素

    :param elements: 要分组的元素列表
    :param group_size: 每组的大小，默认为50
    :return: 逐个返回每组元素
    """
    for i in range(0, len(elements), group_size):
        yield elements[i : i + group_size]

